/*
 * 类名称:InvoicingController.java
 * 包名称:com.platform.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-08-28 13:43:58        lipengjun     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.controller;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.annotation.SysLog;
import com.platform.entity.*;
import com.platform.service.GoodsService;
import com.platform.service.ProductService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.controller.AbstractController;
import com.platform.service.InvoicingService;
import com.platform.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 平台进销记录表Controller
 *
 * @author sunkaichun
 * @date 2019-08-28 13:43:58
 */
@RestController
@RequestMapping("invoicing")
public class InvoicingController extends AbstractController {
    @Autowired
    private InvoicingService invoicingService;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProductService productService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("invoicing:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<InvoicingEntity> list = invoicingService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询平台进销记录表
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("invoicing:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<InvoicingEntity> invoicingEntities = invoicingService.queryList(query);
        int total = invoicingService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(invoicingEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 分页查询库存记录
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/stockList")
    @RequiresPermissions("invoicing:stockList")
    public R stockList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<StockEntity> invoicingEntities = invoicingService.queryStockList(query);
        int total = invoicingService.queryStockListTotal(query);
        PageUtils pageUtil = new PageUtils(invoicingEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("invoicing:info")
    public R info(@PathVariable("id") Integer id) {
        InvoicingEntity invoicing = invoicingService.getById(id);

        return R.ok().put("invoicing", invoicing);
    }

    /**
     * 新增平台进销记录表
     *
     * @param invoicing invoicing
     * @return R
     */
    @SysLog("新增平台进销记录表")
    @RequestMapping("/save")
    @RequiresPermissions("invoicing:save")
    public R save(@RequestBody InvoicingEntity invoicing) {

        invoicingService.add(invoicing);
        return R.ok();
    }

    /**
     * 修改平台进销记录表
     *
     * @param invoicing invoicing
     * @return R
     */
    @SysLog("修改平台进销记录表")
    @RequestMapping("/update")
    @RequiresPermissions("invoicing:update")
    public R update(@RequestBody InvoicingEntity invoicing) {

        invoicingService.update(invoicing);
        return R.ok();
    }

    /**
     * 根据主键删除平台进销记录表
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除平台进销记录表")
    @RequestMapping("/delete")
    @RequiresPermissions("invoicing:delete")
    public R delete(@RequestBody Integer[] ids) {
        invoicingService.deleteBatch(ids);

        return R.ok();
    }

    /**
     *  根据@时间区间，@学校名称，@商铺名称 查询这段时间该店铺的营销额
     */
    @SysLog("查询店铺维度的进销统计表")
    @RequestMapping("/shopCounting")
    @RequiresPermissions("invoicing:list")
    public R shopCounting(@RequestParam Map<String, Object> params){
        Page page = invoicingService.shopCounting(params);
        PageUtils pageUtil = new PageUtils(
                page.getRecords(),
                (int)page.getTotal(),
                (int)page.getSize(),
                (int)page.getCurrent());
        return R.ok().put("page", pageUtil);
    }
    /**
     *  根据@时间区间，@学校名称，@商铺名称 查询这段时间该店铺的营销额
     */
    @SysLog("查询商铺维度的进销统计表")
    @RequestMapping("/goodsCounting")
    @RequiresPermissions("invoicing:list")
    public R goodsCounting(@RequestParam Map<String, Object> params){
        Page page = invoicingService.goodsCounting(params);
        PageUtils pageUtil = new PageUtils(
                page.getRecords(),
                (int)page.getTotal(),
                (int)page.getSize(),
                (int)page.getCurrent());
        return R.ok().put("page", pageUtil);
    }
}
